<template>
  <div class="login-container">
    <el-form :model="loginForm" :rules="loginRules" ref="loginForm">
      <div class="title-container">
        <h3 class="login-title">系统登录</h3>
      </div>
      <el-form-item prop="username">
        <i class="el-icon-user-solid"></i>
        <el-input v-model="loginForm.username" />
      </el-form-item>
      <el-form-item prop="password">
        <i class="el-icon-unlock"></i>
        <el-input v-model="loginForm.password" type="password" />
      </el-form-item>
      <el-form-item>
        <el-button :loading="loading" type="primary" @click="loginHandle" style="width: 100%">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loading: false,
      loginForm: {
        username: "",
        password: ""
      },
      loginRules: {
        username: [{ required: true, message: "请输入账号", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }]
      },
      redirect: undefined
    };
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  methods: {
    loginHandle() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          this.$store.dispatch("user/login", this.loginForm).then(() => {
            this.loading = false;
            this.$router.push({ path: this.redirect || "/" }).catch(()=>{
              this.$router.push({ path: this.redirect || "/" })
            });
          }).catch( () =>{
            this.loading = false;
          });
        }
      });
    }
  }
};
</script>

<style lang="scss" >
$bg: #2d3a4b;

.login-container {
  height: 100%;
  width: 100%;
  background-color: $bg;

  [class^=el-icon-] {
    color: white;
    font-size: 20px;
    margin-left: 10px;
  }

  .el-form {
    width: 25%;
    margin: auto;
    padding: 160px;
  }
  .el-form-item {
    background: rgba(0, 0, 0, 0.1);
  }

  .login-title {
    text-align: center;
    margin: 0px auto 40px auto;
    color: white;
    font-size: 26px;
  }

  .svg {
    color: #889aa4;
    padding: 5px 12px 5px 15px;
  }

  .el-input {
    width: 90%;

    input {
      background: transparent;
      border: 0px;
      color: white;
    }
  }
}
</style>